using UnityEngine;

public class InventoryUIController : MonoBehaviour
{
    public DynamicInventoryDisplay chestPanel; // 箱子仓库面板
    public DynamicInventoryDisplay PlayerBackpackPanel; // 玩家背包面板

    private void Awake()
    {
        chestPanel.gameObject.SetActive(false);
        PlayerBackpackPanel.gameObject.SetActive(false);
    }

    private void OnEnable()
    {
        // 当脚本启用时，监听动态物品界面请求事件
        InventoryHolder.OnDynamicInventoryDisplayRequested += DisplayInventory;
        PlayerInventoryHolder.OnPlayerBackpackDisplayRequested += DisplayPlayerInventory;
    }

    private void OnDisable()
    {
        // 当脚本禁用时，取消监听动态物品界面请求事件
        InventoryHolder.OnDynamicInventoryDisplayRequested -= DisplayInventory;
        PlayerInventoryHolder.OnPlayerBackpackDisplayRequested -= DisplayPlayerInventory;
    }

    void Update()
    {
        //activeInHierarchy 检查该对象是否处于活动状态
        if (chestPanel.gameObject.activeInHierarchy && Input.GetKeyDown(KeyCode.Escape))
        {
            // 按下 ESC 键关闭物品界面
            chestPanel.gameObject.SetActive(false);
        }
        //activeInHierarchy 检查该对象是否处于活动状态
        if (PlayerBackpackPanel.gameObject.activeInHierarchy && Input.GetKeyDown(KeyCode.Escape))
        {
            // 按下 ESC 键关闭物品界面
            PlayerBackpackPanel.gameObject.SetActive(false);
        }
    }

    // 显示背包界面
    void DisplayInventory(InventorySystem invToDisplay, int offset)
    {
        // 激活物品界面面板并刷新显示内容
        chestPanel.gameObject.SetActive(true);
        chestPanel.RefreshDynamicInventory(invToDisplay, offset);
    }

    void DisplayPlayerInventory(InventorySystem invToDisplay, int offset)
    {
        PlayerBackpackPanel.gameObject.SetActive(true);
        PlayerBackpackPanel.RefreshDynamicInventory(invToDisplay, offset);
    }
}